package com.smartcommunity.base.common;

import com.smartcommunity.redis.RedisHashMap;
import com.smartcommunity.bean.constant.Constant;
import com.smartcommunity.bean.constant.RedisKey;
import com.smartcommunity.bean.entity.SysEnum;
import com.smartcommunity.bean.entity.Village;
import com.smartcommunity.dao.common.SystemEnumDao;
import com.smartcommunity.dao.village.VillageDao;
import com.smartcommunity.entity.village.OrganizationVillage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;


/**
 * 缓存公共类
 */
@Component
@Slf4j
public class CacheMap {

    /**
     * 小区信息
     */
    public static final RedisHashMap<String, Village> VILLAGE_INFO_MAP = new RedisHashMap<>(RedisKey.VILLAGE_INFO_MAP, Village.class);
    /**
     * 人员
     */
    public static final RedisHashMap<String, Long> STATISTICS_MAP = new RedisHashMap<>(RedisKey.STATISTICS_MAP, Long.class);

    /**
     * 行政单位
     */
    public static final RedisHashMap<Integer, OrganizationVillage> VILLAGE_ORG_MAP = new RedisHashMap<>(RedisKey.VILLAGE_ORG_MAP, OrganizationVillage.class);

    /**
     * 数据权限配置缓存
     */
    public static final RedisHashMap<String, String> REL_ROLE_VILLAGE = new RedisHashMap<>(RedisKey.ROLE_VILAGE_HASH, String.class, false);


    /**
     * 人员类型
     */
    public static final RedisHashMap<Integer, String> PEOPLE_TYPE_MAP = new RedisHashMap<>(RedisKey.PEOPLE_TYPE_MAP, String.class);
    public static final RedisHashMap<String, Integer> PEOPLE_TYPE_NAME_MAP = new RedisHashMap<>(RedisKey.PEOPLE_TYPE_NAME_MAP, Integer.class);

    /**
     * 证件类型
     */
    public static final RedisHashMap<Integer, String> CREDENTIAL_TYPE_MAP  = new RedisHashMap<>(RedisKey.CREDENTIAL_TYPE_MAP, String.class);
    public static final RedisHashMap<String, Integer> CREDENTIAL_TYPE_NAME_MAP  = new RedisHashMap<>(RedisKey.CREDENTIAL_TYPE_NAME_MAP, Integer.class);

    /**
     * 性别类型
     */
    public static final RedisHashMap<Integer, String> GENDER_MAP = new RedisHashMap<>(RedisKey.GENDER_MAP, String.class);
    public static final RedisHashMap<String, Integer> GENDER_NAME_MAP = new RedisHashMap<>(RedisKey.GENDER_NAME_MAP, Integer.class);

    /**
     * 房屋类型
     */
    public static final RedisHashMap<Integer, String> HOUSE_TYPE_MAP = new RedisHashMap<>(RedisKey.HOUSE_TYPE_MAP, String.class);
    public static final RedisHashMap<String, Integer> HOUSE_NAME_MAP = new RedisHashMap<>(RedisKey.HOUSE_NAME_MAP, Integer.class);

    /**
     * 民族字典
     */
    public static final RedisHashMap<Integer, String> NATION_MAP = new RedisHashMap<>(RedisKey.NATION_MAP, String.class);
    public static final RedisHashMap<String, Integer> NATION_NAME_MAP = new RedisHashMap<>(RedisKey.NATION_NAME_MAP, Integer.class);

    /**
     * 门禁设备类型
     */
    public static final RedisHashMap<Integer, String> ACCESS_TYPE_MAP = new RedisHashMap<>(RedisKey.ACCESS_TYPE_MAP, String.class);

    /**
     * 门禁开门方式
     */
    public static final RedisHashMap<Integer, String> OPEN_TYPE_MAP = new RedisHashMap<>(RedisKey.OPEN_TYPE_MAP, String.class);

    /**
     * 摄像机类型
     */
    public static final RedisHashMap<Integer, String> CAMERA_TYPE_MAP = new RedisHashMap<>(RedisKey.CAMERA_TYPE_MAP, String.class);

    /**
     * 摄像机功能类型
     */
    public static final RedisHashMap<Integer, String> CAMERAFUN_TYPE_MAP = new RedisHashMap<>(RedisKey.CAMERAFUN_TYPE_MAP, String.class);

    /**
     * 设备厂商
     */
    public static final RedisHashMap<Integer, String> DEVICE_VENDOR_MAP = new RedisHashMap<>(RedisKey.DEVICE_VENDOR_MAP, String.class);

    /**
     * 设备状态
     */
    public static final RedisHashMap<Integer, String> DEVICE_STATUS_MAP = new RedisHashMap<>(RedisKey.DEVICE_STATUS_MAP, String.class);

    /**
     * 设备类型
     */
    public static final RedisHashMap<Integer, String> DEVICE_TYPE_MAP = new RedisHashMap<>(RedisKey.DEVICE_TYPE_MAP, String.class);

    /**
     * 人员居住类型
     */
    public static final String RESIDENTIA_TYPE = "residentiaType";
    public static final RedisHashMap<Integer, String> RESIDENTIA_TYPE_MAP = new RedisHashMap<>(RESIDENTIA_TYPE, String.class);


    @Autowired
    private VillageDao villageDao;

    @Autowired
    private SystemEnumDao SystemEnumDao;

    /**
     * 初始化缓存
     */
    public void initCache() {
        try {
            initVillageInfo();
            initVillageOrgInfo();
            initSysEnum();
        } catch (Exception e) {
            log.error("redis缓存数据异常", e);
        }
    }

    /**
     * 缓存小区信息
     */
    private void initVillageInfo() {
        log.info("开始缓存小区信息！");
        List<Village> villageList = villageDao.getAllVillageInfo();
        for (Village village : villageList) {
            if (village != null && !StringUtils.isEmpty(village.getVillageCode())) {
                VILLAGE_INFO_MAP.put(village.getVillageCode(), village);
            }
        }
        log.info("缓存小区信息结束！");
    }

    /**
     * 缓存小区行政区划信息
     */
    private void initVillageOrgInfo(){
        List<OrganizationVillage> orgVillages = villageDao.getVillageOrgList(new HashMap<>());
        for (OrganizationVillage village : orgVillages) {
            if (village != null && village.getId() != null ) {
                VILLAGE_ORG_MAP.put(village.getId(), village);
            }
        }
        log.info("缓存小区行政区划信息结束！");
    }

    private void initSysEnum() {
        log.info("开始缓存枚举信息！");
        List<SysEnum> enumList = SystemEnumDao.selectEnumList();
        for(SysEnum sysEnum : enumList){
            if(sysEnum == null){
                continue;
            }
            String name = sysEnum.getLabel();
            String value =sysEnum.getValue();
            //人员类型
            if(Constant.PEOPLETYPE.equals(sysEnum.getType())){
                PEOPLE_TYPE_MAP.put(Integer.parseInt(value),name);
                PEOPLE_TYPE_NAME_MAP.put(name,Integer.parseInt(value));
            //证件类型
            }else if(Constant.CREDENTIALTYPE.equals(sysEnum.getType())){
                CREDENTIAL_TYPE_MAP.put(Integer.parseInt(value),name);
                CREDENTIAL_TYPE_NAME_MAP.put(name,Integer.parseInt(value));
            //性别
            }else if(Constant.GENDER.equals(sysEnum.getType())){
                GENDER_MAP.put(Integer.parseInt(value),name);
                GENDER_NAME_MAP.put(name,Integer.parseInt(value));
            //民族
            }else if(Constant.NATION.equals(sysEnum.getType())){
                NATION_MAP.put(Integer.parseInt(value),name);
                NATION_NAME_MAP.put(name,Integer.parseInt(value));
            //房屋类型
            }else if(Constant.HOUSETYPE.equals(sysEnum.getType())){
                HOUSE_TYPE_MAP.put(Integer.parseInt(value),name);
                HOUSE_NAME_MAP.put(name,Integer.parseInt(value));
            //门禁设备类型
            }else if(Constant.ACCESSTYPE.equals(sysEnum.getType())){
                ACCESS_TYPE_MAP.put(Integer.parseInt(value),name);
            //门禁开门方式
            }else if(Constant.OPENTYPE.equals(sysEnum.getType())){
                OPEN_TYPE_MAP.put(Integer.parseInt(value),name);
            //摄像机类型
            }else if(Constant.CAMERATYPE.equals(sysEnum.getType())){
                CAMERA_TYPE_MAP.put(Integer.parseInt(value),name);
            //摄像机功能类型
            }else if(Constant.CAMERAFUNTYPE.equals(sysEnum.getType())){
                CAMERAFUN_TYPE_MAP.put(Integer.parseInt(value),name);
            //设备厂商
            }else if(Constant.DEVICEVENDOR.equals(sysEnum.getType())){
                DEVICE_VENDOR_MAP.put(Integer.parseInt(value),name);
            //设备状态
            }else if(Constant.DEVICESTATUS.equals(sysEnum.getType())){
                DEVICE_STATUS_MAP.put(Integer.parseInt(value),name);
            //设备类型
            }else if(Constant.DEVICESTYPE.equals(sysEnum.getType())){
                DEVICE_TYPE_MAP.put(Integer.parseInt(value),name);
            //人员居住类型
            }else if(RESIDENTIA_TYPE.equals(sysEnum.getType())){
                RESIDENTIA_TYPE_MAP.put(Integer.parseInt(value),name);
            }
        }
        log.info("缓存枚举信息结束！");
    }

}
